package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.tianji.TianjiAntifraudData;
import com.ucredit.babel.common.model.tianji.TianjiAntifraudRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 天机反欺诈接口V1
 *
 * @author renzhanzheng
 */

public class TianjiAntifraudParse extends ParseBase{
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 天机反欺诈接口V1
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */

    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TianjiAntifraudRecord obj = (TianjiAntifraudRecord)model;
        TianjiAntifraudData data = obj.getData();
        JSONObject info =data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());
        JSONObject tianji = info.getJSONObject("tianji_api_agentce_antifraud_response") == null ? new JSONObject() : info.getJSONObject("tianji_api_agentce_antifraud_response");
        JSONArray risknfoArray = tianji.getJSONArray("riskInfo") == null ? new JSONArray() : tianji.getJSONArray("riskInfo");

        StringBuilder sb = getBaseInfo(obj, successful);
        //错误码
        sb.append(info.getString("error") == null ? "" : info.getString("error")).append(CRT001);
        //错误信息
        sb.append(info.getString("msg") == null ? "" : info.getString("msg")).append(CRT001);
        //请求ID
        sb.append(info.getString("request_id") == null ? "" : info.getString("request_id")).append(CRT001);
        //该条记录能否查到
        sb.append(tianji.getString("found") == null ? "" : tianji.getString("found")).append(CRT001);
        //该条记录中的身份证能否查到
        sb.append(tianji.getString("idFound") == null ? "" : tianji.getString("idFound")).append(CRT001);
        //欺诈分值
        sb.append(tianji.getString("riskScore") == null ? "" : tianji.getString("riskScore")).append(CRT001);

        StringBuilder detailSb = new StringBuilder(500);
        int i = 0;
        for (Object o : risknfoArray){
            i++;
            detailSb.delete(0, detailSb.length());
            JSONObject v = (JSONObject)o;
            //序号
            detailSb.append(i).append(CRT001);
            //风险码等级
            detailSb.append(v.getString("riskCodeValue") == null ? "" : v.getString("riskCodeValue")).append(CRT001);
            //风险码
            detailSb.append(v.getString("riskCode") == null ? "" : v.getString("riskCode")).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + detailSb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        if (i == 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tianji_fraudv1_inc";
    }

    @Override
    public String getType() {
        return RecordType.TIANJI_ANTIFRAUD_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "天机反欺诈接口V1";
    }
}
